package com.atguigu.blog.controller;

import com.atguigu.blog.pojo.Article;
import com.atguigu.blog.pojo.Category;
import com.atguigu.blog.service.impl.ArticleServiceImpl;
import com.atguigu.blog.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author MikeCoder
 * @create 2023-11-2421:07
 * @description:
 * @verson:
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
@Tag(name = "文章接口",description = "有关文章的接口")
public class ArticleController {
    @Autowired
    private ArticleServiceImpl articleService;

    @PostMapping
    @Operation(summary = "通过分类id查询文章集合")
    @Parameters(@Parameter(name = "category",description = "分类对象"))
    public Result<List<Article>> showArticleByCid(@RequestBody Category category){
        List<Article> articleList = articleService.showArticleByCid(category);
        return Result.ok(articleList);
    }

    @GetMapping("/{id}")
    @Operation(summary = "通过文章id查询文章")
    @Parameters(@Parameter(name = "id",description = "文章id"))
    public Result<Article> showArticleById(@PathVariable Long id){
        Article article = articleService.showArticleById(id);
        return Result.ok(article);
    }

    @GetMapping("/show/{uid}")
    @Operation(summary = "通过uid查询文章")
    @Parameters(@Parameter(name = "uid",description = "用户id"))
    public Result<List<Article>> showArticleByUid(@PathVariable Long uid){
        List<Article> articleList = articleService.showArticleByUid(uid);
        return Result.ok(articleList);
    }

    @PostMapping("/add")
    @Operation(summary = "添加文章")
    @Parameters(@Parameter(name = "article",description = "文章对象"))
    public Result<Integer> add(@RequestBody Article article){
        Integer add = articleService.add(article);
        if(add>0){
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    @PutMapping("/update")
    @Operation(summary = "修改文章")
    @Parameters(@Parameter(name = "article",description = "文章对象"))
    public Result<Integer> change(@RequestBody Article article){
        Integer add = articleService.change(article);
        if(add>0){
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除文章")
    @Parameters(@Parameter(name = "id",description = "文章id"))
    public Result<Integer> change(@PathVariable Long id){
        Integer result = articleService.remove(id);
        if(result>0){
            return Result.ok(null);
        }
        return Result.fail(null);
    }
}
